//
//  LXDTabBar.m
//  lxd-inke
//
//  Created by ma c on 16/9/1.
//  Copyright © 2016年 lxd. All rights reserved.
//

#import "LXDTabBar.h"

@interface LXDTabBar ()

@property (nonatomic, strong) UIImageView * tabbgView;

@property (nonatomic, strong) NSArray * datalist;

@property (nonatomic, strong) UIButton * lastItem;

@property (nonatomic, strong) UIButton * cameraButton;
@end

@implementation LXDTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        [self addSubview:self.tabbgView];
        
        for (NSInteger i = 0; i < self.datalist.count; i++) {
            
            UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
            // 管理高亮状态（不让图片点击时，变成高亮状态）
            item.adjustsImageWhenHighlighted = NO;
            
            [item setImage:[UIImage imageNamed:self.datalist[i]] forState:UIControlStateNormal];
           
            [item setImage:[UIImage imageNamed:[self.datalist[i] stringByAppendingString:@"_p"]] forState:UIControlStateSelected];
            
            [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
            
            item.tag = LXDItemTypeLive + i;
            
            if (i == 0) {
                item.selected = YES;
                self.lastItem = item;
            }
            
            [self addSubview:item];
        }
        //  添加直播按钮
        [self addSubview:self.cameraButton];
    }
    return self;
}
// 初始化Button的frame
- (void)layoutSubviews {

    [super layoutSubviews];
    
    self.tabbgView.frame = self.bounds;
    
    CGFloat width = self.bounds.size.width / self.datalist.count;

    for (NSInteger i = 0; i < [self subviews].count; i++) {
        
        UIView * btn = [self subviews][i];
        
        if ([btn isKindOfClass:[UIButton class]]) {
            
            btn.frame = CGRectMake((btn.tag - LXDItemTypeLive) * width, 0, width, self.frame.size.height);
        }
    }
    [self.cameraButton sizeToFit];
    self.cameraButton.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height - 50);
}

- (void)clickItem:(UIButton *)button {

    if ([self.delegate respondsToSelector:@selector(tabbar:clickButton:)]) {
        [self.delegate tabbar:self clickButton:button.tag];
    }
    
//    !self.block?:self.block(self,button.tag);// 与下面三句效果等同
    if (self.block) {
        self.block(self,button.tag);
    }
    // 切换按钮的选中状态（在初始化Button时，默认第一个为选中状态，并获取到最后选中的按钮，触发点击事件时，选中的按钮会发生改变，则最后处于选中状态的按钮的selected就要置为NO，当前点击的按钮设为选中状态，当前选中按钮也是最后选中的按钮）
    if (button.tag == LXDItemTypeLaunch) {
        return;
    }
    
    self.lastItem.selected = NO;
    button.selected = YES;
    self.lastItem = button;
    
    // 点击时设置动画
    [UIView animateWithDuration:0.2 animations:^{
       //将Button放大1.2倍
        button.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            // 恢复原始状态
            button.transform = CGAffineTransformIdentity;
        }];
        
    }];
}

- (UIButton *)cameraButton {
    if (!_cameraButton) {
        _cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cameraButton setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];
        _cameraButton.tag = LXDItemTypeLaunch;
        [_cameraButton sizeToFit];
        [_cameraButton addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cameraButton;
}

- (NSArray *)datalist {

    if (!_datalist) {
        _datalist = @[@"tab_live",@"tab_me"];
    }
    return _datalist;
}

-(UIImageView *)tabbgView {

    if (!_tabbgView) {
        _tabbgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"global_tab_bg"]];
    }
    return _tabbgView;
}
@end
